/**
 *   providerSelects.js 为物流供应商多选控件
 *   物流供应商控件的使用方式: 在需要加入物流供应商下拉列表的地方 直接写   <div  id = "providerSelectsPluginsDiv"></div>
 *   可以定义事件监听变化  function topChannelChange(obj){};
 *
 **/
var providerMap = new Map();
(function () {
    $X("/logistics/ProviderConfService", "list").callx({
        onResult: function (result) {
            var plug = $('div[name="providerSelectsPluginsDiv"]');
            if (plug.length === 0) {
                throw new Error('没有找到插件配置!');
            }
            var thisPlug = plug[0];
            if (thisPlug.dataset.selectedUrl) {
                var url = thisPlug.dataset.selectedUrl;
                var lastindex = url.lastIndexOf("/");

                $X(url.substring(0, lastindex), url.substring(lastindex + 1, url.length)).callx({
                    onResult: function (selected) {
                        init(result, selected);
                    },
                    onError: function (error) {

                    }
                });
            } else {
                init(result);
            }


        },
        onError: function (error) {
        }
    });

    function init(result, selected) {
        var html = '<select style="width:100%" id="providerSelectsId" name="logisticsType" class="form-control">';
        html += '<option value="">--请选择--</option>';
        var option = "";
        for (var i = 0; i < result.length; i++) {
            providerMap.put(result[i].providerCode, result[i].providerName);
            if (selected) {
                option += '<option value=' + result[i].providerCode + ' ' + (selected.indexOf(result[i].providerCode) >= 0 ? 'selected="selected"' : '') + '>' + result[i].providerName + '</option>';
            } else {
                option += '<option value=' + result[i].providerCode + '>' + result[i].providerName + '</option>';
            }
        }
        html += option;
        html += '</select>';
        $(html).appendTo('div[name="providerSelectsPluginsDiv"]');

    }
})()
